package com.older.wptsb.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.older.wptsb.domain.dto.CommunityDTO;
import com.older.wptsb.domain.dto.UserCommunityRegisterDTO;
import com.older.wptsb.domain.dto.UserDTO;
import com.older.wptsb.domain.dto.UserRegisterDTO;
import com.older.wptsb.domain.po.RequireRole;
import com.older.wptsb.domain.po.User;
import com.older.wptsb.domain.po.UserRole;
import com.older.wptsb.domain.vo.CommunityVO;
import com.older.wptsb.service.CommunityService;
import com.older.wptsb.service.UserService;
import com.older.wptsb.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminCommunityController {
    @Resource
    private CommunityService communityService;
    @Resource
    private UserService userService;

    @RequireRole({UserRole.ROLE_ADMIN})
    @PostMapping("/addCommunityUser")
    public Result<String> addCommunityUser(@RequestBody UserCommunityRegisterDTO userCommunityRegisterDTO ){
        userService.register1(userCommunityRegisterDTO);
        return Result.success("添加成功");
    }

    @DeleteMapping("/deleteCommunityUser/{userId}")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<String> deleteCommunityUser(@PathVariable Integer userId){
        userService.removeById(userId);
        return Result.success("删除成功");
    }

    @GetMapping("/getCommunityUsers/{communityId}")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<List<User>> getCommunityUsers(@PathVariable Integer communityId){
        List<User> users = userService.list(new QueryWrapper<User>()
                .eq("role_type", "community")
                .eq("community_id", communityId));
        return Result.success(users);
    }

    @PutMapping("/updateCommunityUser")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<String> updateCommunityUser(@RequestBody UserDTO userDTO){
        userService.updateById(userDTO);
        return Result.success("修改成功");
    }
    @GetMapping("/getCommunityList")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<List<CommunityVO>> getCommunityList(){
        List<CommunityVO> communityVOList = communityService.getCommunityList();
        return Result.success(communityVOList);
    }

    @GetMapping("/getCommunity/{id}")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<CommunityVO> getCommunity(@PathVariable Integer id){
        CommunityVO communityVO = communityService.getCommunity(id);
        return Result.success(communityVO);
    }
    @PutMapping("/updateCommunity")
    @RequireRole({UserRole.ROLE_ADMIN})
        public Result<String> updateCommunity(@RequestBody CommunityDTO communityDTO){
        communityService.updateCommunity(communityDTO);
        return Result.success("修改成功");
    }
    @PostMapping("/addCommunity")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<String> addCommunity(@RequestBody CommunityDTO communityDTO){
        communityService.addCommunity(communityDTO);
        return Result.success("添加成功");
    }
    @DeleteMapping("/deleteCommunity/{id}")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<String> deleteCommunity(@PathVariable Integer id){
        communityService.removeById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/getCommunityPeople")
    @RequireRole({UserRole.ROLE_ADMIN})
    public Result<Integer> getCommunityPeople(){
        int count = userService.count(new QueryWrapper<User>().isNotNull("community_id"));
        return Result.success(count);
    }
}
